﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Projeto.Models;
using OnBarcode.Barcode;
using System.Drawing;
using System.Drawing.Imaging;

namespace Projeto.Controllers
{
    public class CodigoQRController : Controller
    {
        private EstacionamentoEntities db = new EstacionamentoEntities();

        #region "Configuração de Página"

        //
        // GET: /CodigoQR/

        public ActionResult Index()
        {
            return View(db.CodigoQR.ToList());
        }

        //
        // GET: /CodigoQR/Details/5

        public ActionResult Details(int id = 0)
        {
            CodigoQR codigoqr = db.CodigoQR.Find(id);
            if (codigoqr == null)
            {
                return HttpNotFound();
            }
            return View(codigoqr);
        }

        //
        // GET: /CodigoQR/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /CodigoQR/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(CodigoQR codigoqr)
        {
            if (ModelState.IsValid)
            {
                db.CodigoQR.Add(codigoqr);
                db.SaveChanges();
                return RedirectToAction("Details", new { Id = codigoqr.Id });
            }

            return View(codigoqr);
        }

        //
        // GET: /CodigoQR/Edit/5

        public ActionResult Edit(int id = 0)
        {
            CodigoQR codigoqr = db.CodigoQR.Find(id);
            if (codigoqr == null)
            {
                return HttpNotFound();
            }
            return View(codigoqr);
        }

        //
        // POST: /CodigoQR/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(CodigoQR codigoqr)
        {
            if (ModelState.IsValid)
            {
                db.Entry(codigoqr).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Details", new { Id = codigoqr.Id });
            }
            return View(codigoqr);
        }

        //
        // GET: /CodigoQR/Delete/5

        public ActionResult Delete(int id = 0)
        {
            CodigoQR codigoqr = db.CodigoQR.Find(id);
            if (codigoqr == null)
            {
                return HttpNotFound();
            }
            return View(codigoqr);
        }

        //
        // POST: /CodigoQR/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            CodigoQR codigoqr = db.CodigoQR.Find(id);
            db.CodigoQR.Remove(codigoqr);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        public ActionResult GetQRCode(int id) 
        {
            CodigoQR codigoqr = db.CodigoQR.Find(id);

            byte[] img = GerarImagemCodigoQR(codigoqr.Valor);

            return File(img, "image/jpeg");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
        
        #endregion

        #region "Métodos dos Controles"

        private byte[] GerarImagemCodigoQR(string valor)
        {
            QRCode gerador = new QRCode();

            gerador.Data = valor;

            gerador.ImageFormat = ImageFormat.Jpeg;
            gerador.X = 10;

            byte[] Img = gerador.drawBarcodeAsBytes();

            return Img;
        }

        #endregion

    }
}